package com.yunshu.yunshuapp.zixunlist;

import android.app.Activity;
import android.content.SharedPreferences;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yunshu.yunshuapp.R;
import com.yunshu.yunshuapp.utils.YSUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by zhouwenjie on 2016/12/22.
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class ZixunListModel {

//    public static final String List_ID_HANGYEYAOWEN = "1";
    public static final String List_ID_REDIANZIXUN = "2";
//    public static final String List_ID_SHICHANGGUANCHA = "3";
    public static final String List_ID_CHANGSHANGJIAGE = "4"; //厂商价格
//    public static final String List_ID_SHICHANGHANGQING = "5"; //市场行情

    @JsonIgnoreProperties
    private boolean isHeader;
    @JsonIgnoreProperties
    private boolean isBanner;

    @JsonIgnoreProperties
    private int imagePath;

    @JsonProperty("id")
    private String modelId;

    @JsonProperty("ext")
    private String imageUrl;
    private String title;

    @JsonProperty("praiseNum")
    private String favourateCount;
    @JsonProperty("createtime")
    private String date;

    @JsonProperty("type")
    public String listId;

    public static ZixunListModel getHeader(String listId){
        ZixunListModel header1 = new ZixunListModel();
        header1.setHeader(true);
//        if (listId == List_ID_HANGYEYAOWEN) {
//            header1.setTitle("行业要闻");
//            header1.setImagePath(R.mipmap.home_list_icon1);
//        }else if(listId == List_ID_REDIANZIXUN) {
//            header1.setTitle("热点资讯");
//            header1.setImagePath(R.mipmap.home_list_icon2);
//        }else if(listId == List_ID_SHICHANGGUANCHA) {
//            header1.setTitle("市场观察");
//            header1.setImagePath(R.mipmap.home_list_icon3);
//        }
        header1.listId = listId;
        return header1;
    }

    public boolean isHeader() {
        return isHeader;
    }

    public void setHeader(boolean header) {
        isHeader = header;
    }

    public int getImagePath() {
        return imagePath;
    }

    public void setImagePath(int imagePath) {
        this.imagePath = imagePath;
    }

    public String getModelId() {
        return modelId;
    }

    public void setModelId(String modelId) {
        this.modelId = modelId;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getFavourateCount() {
        return favourateCount;
    }

    public void setFavourateCount(String favourateCount) {
        this.favourateCount = favourateCount;
    }

    public String getDate() {
        return YSUtils.getDate(date);
    }

    public void setDate(String date) {
        this.date = date;
    }

    public boolean isBanner() {
        return isBanner;
    }

    public void setBanner(Boolean banner) {
        isBanner = banner;
    }

    public String serialize() {
        String zixunStr = null ;
        ObjectMapper mapper = new ObjectMapper();
        try {
            zixunStr = mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return zixunStr;
    }


    static ArrayList<String> loveList;
    public Boolean isLoved(Activity activity){
        if (loveList == null) {
            //获取SharedPreferences对象
            SharedPreferences sp = activity.getSharedPreferences("SP", MODE_PRIVATE);
            String readListStr = sp.getString("ssssLoveList", "");
            String[] result = readListStr.split(",");
            loveList = new ArrayList<String>();
            for (String id : result)
                loveList.add(id);
        }
        for (String id : loveList){
            if (id.equalsIgnoreCase(this.modelId))
                return true;
        }
        return false;
    }

    private static final int MODE_PRIVATE = 0;
    public void setLoved(Boolean isLoved,Activity activity){
        if (loveList == null) {
            //获取SharedPreferences对象
            SharedPreferences sp = activity.getSharedPreferences("SP", MODE_PRIVATE);
            String readListStr = sp.getString("qqqqLoveList", "");
            String[] result = readListStr.split(",");
            loveList = new ArrayList<String>();
            for (String id : result)
                loveList.add(id);
        }
        if (isLoved) {
            loveList.add(this.modelId);
        }else {
            int deleteIndex = -1;
            for (int i = 0 ;i <loveList.size(); i++){
                String id = loveList.get(i);
                if (id.equalsIgnoreCase(this.modelId))
                    deleteIndex = i;
            }
            if (deleteIndex >=0)
                loveList.remove(deleteIndex);
        }

        StringBuffer result = new StringBuffer();
        if (loveList != null){
            for (String id : loveList) {
                result.append(id);
                result.append(",");
            }
        }
        SharedPreferences sp = activity.getSharedPreferences("SP", MODE_PRIVATE);
        //存入数据
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("qqqqLoveList", result.toString());
        editor.commit();

    }
}
